<?php

namespace app\index\controller;
use app\index\model\Collection;
use app\index\model\Examine;
use app\index\model\Official;

use app\common\controller\Basecontroller;
use app\validate\Vali;
use think\Request;

class Details extends Basecontroller {
    /**
     * 显示职位详情接口：引用职位表中的查询方法，输出相应code
     * @copyright hyw (接口未完善)
     * @return \think\response\Json
     */
    public function details(Request $request,Official $official,Examine $exam,Collection $coll) {
        $user_id = $this->getUserID();
        $official_id = $request->param('officialId');
        $data = $official->getDetailAttr($official_id);
        $examStatus = count($exam->where([['user_id','=',$user_id],['official_id','=',$official_id]])->select());
        $examStatus = ['examStatus' => $examStatus];
        $collStatus = count($coll->where([['user_id','=',$user_id],['official_id','=',$official_id]])->select());
        $collStatus = ['collStatus' => $collStatus];
        return $this->returnJson(self::OK,'输出成功',[$data,$examStatus,$collStatus]);
    }

    /**
     * 立即投档接口：接收前端传的id，存入数据库
     * @copyright hyw
     * @param Request $request
     * @param Examine $examine
     * @return \think\response\Json
     */
    public function throwNow(Request $request,Examine $examine,Vali $vali) {
        $user_id = $this->getUserID();
        $official_id = $request->param('officialId');
        $data = [
            'user_id'     => $user_id,
            'official_id' => $official_id,
            'state'          => 2
        ];
        if (!$vali->scene('examChe')->batch()->check($data)) return $this->returnJson(self::ERROR,'上传有误',$vali->getError());
        $examine->insert($data);
        return $this->returnJson(self::JUMP,'跳转到用户投档职位页',$data);
    }

    /**
     * 收藏职位接口：获取前端传的id，存入数据库，进行跳转
     * @copyright hyw
     * @param Request $request
     * @param Collection $collection
     * @return \think\response\Json
     */
    public function collection(Request $request,Collection $collection,Vali $vali) {
        $user_id = $this->getUserID();
        $official_id = $request->param('officialId');
        $data = [
            'user_id'       =>      $user_id,
            'official_id'   =>      $official_id,
        ];
        if (!$vali->scene('examChe')->batch()->check($data)) return $this->returnJson(self::ERROR,'上传有误',$vali->getError());
        $collection->insert($data);
        return $this->returnJson(self::JUMP,'跳转到个人页收藏职位',$data);
    }

    /**
     * 取消收藏接口
     * @copyright hyw
     * @param Request $request
     * @param Collection $collection
     * @param Vali $vali
     * @return \think\response\Json
     */
    public function  cancel(Request $request,Collection $collection,Vali $vali) {
        $user_id = $this->getUserID();
        $official_id = $request->param('officialId');
        $collection->where([['user_id','=',$user_id],['official_id','=',$official_id]])->update(['is_delete' => 1]);
        return $this->returnJson(self::OK,'取消成功');
    }
}